package com.techstar.ats.type;

import java.util.Objects;

public interface Enumerated {

	int ordinal();

	default int value() {
		return this.ordinal();
	}

	String name();

	default String literal() {
		return this.name();
	}

	static <E extends Enum<E> & Enumerated> E valueOf(Class<E> klass, int value) {
		return Objects.requireNonNull(valueOf(klass, value, null),
			() -> String.format("Enumerated(%s) %d not found", klass.getName(), value));
	}

	static <E extends Enum<E> & Enumerated> E valueOf(Class<E> klass, int value, E _default) {
		for(E val : klass.getEnumConstants()) {
			if(val.value() == value) {
				return val;
			}
		}

		return _default;
	}
}
